﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbGenerator.Resources.dicts;

namespace DbGenerator.Utilities
{
    public static class SDbGenerationUtilities
    {
        /*IF NOT EXISTS 
         * (SELECT * FROM sys.objects 
         * WHERE object_id = OBJECT_ID(N'[dbo].[L_Stoklar]') AND type in (N'U'))
         */

        #region public static methods
        public static string GenerateIfNotExistsSentence(string objectName, ESqlObjectType objType)
        {
            string frmt = " IF NOT EXISTS "
                + "(SELECT * FROM sys.objects "
                + "WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'{1}')) ";
            return String.Format(frmt, objectName, SSqlObjectTypes.Get(objType));
        }
        public static string GenerateDbObjectExistsSentence(string objectName, ESqlObjectType objType)
        {
            string frmt = "IF EXISTS "
                + "(SELECT * FROM sys.objects "
                + "WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'{1}'))"
                + "SELECT 1 ELSE SELECT 0";
            return String.Format(frmt, objectName, SSqlObjectTypes.Get(objType));
        }
        public static string ConstructFKConstraintName(string baseColumnName, string referencedTableName)
        {
            string sFactor = Guid.NewGuid().ToString().Replace("-", "");
            return String.Format("FK_{0}_{1}_{2}", baseColumnName, referencedTableName, sFactor);
        }
        #endregion

    }
}
